<?php
class ModelAccountCustomer extends Model {

	public function addCustomer($data) {
		$SQL = "INSERT INTO " . DB_PREFIX . "customer " .
			"SET firstname = '" . $this->db->escape($data['firstname']) . "', " .
				"lastname = '" . $this->db->escape($data['lastname']) . "', " .
				"username = '" . $this->db->escape($data['username']) . "', " .
				"ym = '" . $this->db->escape($data['ym']) . "', " .
				"skype = '" . $this->db->escape($data['skype']) . "', " .
				"email = '" . $this->db->escape($data['email']) . "', " .
				"telephone = '" . $this->db->escape($data['telephone']) . "', " .
				"fax = '" . $this->db->escape($data['fax']) . "', " .
				"password = '" . $this->db->escape(md5($data['password'])) . "', " .
				"newsletter = '" . $this->db->escape($data['newsletter']) . "', " .
				"status = '" . (int)!$this->config->get('config_customer_approval') . "', " .
				"date_added = NOW()";
      	$this->db->query($SQL);
		$customer_id = $this->db->getLastId();

		$SQL = "INSERT INTO " . DB_PREFIX . "address " .
	     " SET customer_id = '" . (int)$customer_id . "', " .
		   "firstname = '" . $this->db->escape($data['firstname']) . "', " .
	   		"lastname = '" . $this->db->escape($data['lastname']) . "', " .
			"company = '" . $this->db->escape($data['company']) . "', " .
			"address_1 = '" . $this->db->escape($data['address_1']) . "', " .
			"address_2 = '" . $this->db->escape($data['address_2']) . "', " .
			"country_id = '" . (int)$data['country_id'] . "', " .
			"zone_id = '" . (int)$data['zone_id'] . "'";
      	$this->db->query($SQL);

		$address_id = $this->db->getLastId();
      	$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
	}

	//----------------------------------------------------------//
	// Insert new user into user table of forum database
	//----------------------------------------------------------//
	public function addUserForum($data) {
		$db_forum = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, 'forum');
		$pass = md5(md5($db_forum->escape($data['password'])).'V.}');
		$sql = "INSERT INTO " . DB_PREFIX . " user " .
				" SET usergroupid = '" . 2 .
				"', username = '" . $db_forum->escape($data['username']) .
				"', password = '" . $pass .
				"', email = '" . $db_forum->escape($data['email']) .
				"', yahoo = '" . $db_forum->escape($data['ym']) .
				"', skype = '" . $db_forum->escape($data['skype']) .
				"', salt = 'V.}" .
				"', joindate = NOW()";
      	$db_forum->query($sql);
	}
	public function editCustomer($data) {
		$SQL = "UPDATE " . DB_PREFIX . "customer " .
				"SET firstname = '" . $this->db->escape($data['firstname']) . "', " .
					"lastname = '" . $this->db->escape($data['lastname']) . "', " .
					"email = '" . $this->db->escape($data['email']) . "', " .
					"telephone = '" . $this->db->escape($data['telephone']) . "', " .
					"fax = '" . $this->db->escape($data['fax']) . "' " .
				"WHERE customer_id = '" . (int)$this->customer->getId() . "'";
		$this->db->query($SQL);
	}

	public function editPassword($email, $password) {
      	$this->db->query("UPDATE " . DB_PREFIX . "customer SET password = '" . $this->db->escape(md5($password)) . "' WHERE email = '" . $this->db->escape($email) . "'");
	}

	public function editNewsletter($newsletter) {
		$this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '" . (int)$newsletter . "' WHERE customer_id = '" . (int)$this->customer->getId() . "'");
	}

	public function getCustomer($customer_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'");

		return $query->row;
	}

	public function getTotalCustomersByEmail($email) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE email = '" . $this->db->escape($email) . "'");
		return $query->row['total'];
	}
	//----------------------------------------------------------------------------------//
	// get customer by username
	//----------------------------------------------------------------------------------//
	public function getTotalCustomersByUserName($username) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "customer WHERE username = '" . $this->db->escape($username) . "'");
		return $query->row['total'];
	}
}
?>